package 栈和队列;

import java.util.Stack;

/**
 * @description:
 * @author: ywk
 * @date: 2020-06-28
 */
public class ReverseStack {
    //逆栈
    public static void reverseStack(Stack<Integer> stack) {
        if (stack.isEmpty()) {
            return;
        }
        int temp = stack.pop();
        reverseStack(stack);
        insertAtBottom(stack, temp);
    }

    private static void insertAtBottom(Stack<Integer> stack, int data) {
        if (stack.isEmpty()) {
            stack.push(data);
            return;
        }
//        int temp = stack.pop();
//        insertAtBottom(stack, data);
        stack.push(data);

    }

    private static void printStack(Stack stack) {
        while (!stack.isEmpty()) {
            System.out.println(stack.pop());
        }
    }

    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        reverseStack(stack);
        printStack(stack);
    }
}
